package wwy.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wwy.bean.Contacts;
import wwy.bean.ContactsExample;
import wwy.bean.ContactsExample.Criteria;
import wwy.mapper.ContactsMapper;

import java.util.List;

/**
 * @description:
 * @author: 九天
 * @time: 2021/10/15 16:32
 */
@Service
public class ContactsService {

    @Autowired
    private ContactsMapper contactsMapper;

    /**
     * 查询所有联系人
     * @return
     */
    public List<Contacts> getAll() {
        return  contactsMapper.selectByExample(null);
    }

    /**
     * 根据id查询联系人
     * @return
     */
    public Contacts getOne(int tid) {
        return contactsMapper.selectByPrimaryKey(tid);
    }


    /**
     * 删除联系人
     * @return
     */
    public void deleteCon(Integer id) {
        contactsMapper.deleteByPrimaryKey(id);
    }

    public void deleteBatch(List<Integer> ids) {
        ContactsExample example = new ContactsExample();
        Criteria criteria = example.createCriteria();
        criteria.andTIdIn(ids);
        contactsMapper.deleteByExample(example);
    }

    /**
     * 联系人保存
     * @param contacts
     */
    public void saveCon(Contacts contacts) {
        contactsMapper.insertSelective(contacts);
    }

    /**
     * 检验联系人名字是否可用
     *
     * @param conName
     * @return  true：代表当前名字可用   fasle：不可用
     */
    public boolean checkUser(String conName) {
        ContactsExample example = new ContactsExample();
        Criteria criteria = example.createCriteria();
        criteria.andTNameEqualTo(conName);
        long count = contactsMapper.countByExample(example);
        return count == 0;
    }

    /**
     * 联系人更新
     * @param contacts
     */
    public void updateCon(Contacts contacts) {
        contactsMapper.updateByPrimaryKeySelective(contacts);
    }
}
